import axios from "axios";
import { Tsrv, ts } from '@/services';
import lexicon from '@/lib/lexicon';
import XEUtils from "xe-utils";
import { del } from "vue";

// import { T_return, I_return } from '@/class/base/return';
// import { T_setDataRes, I_setDataRes } from '@/class/base/setDataRes';
// import Vue from "vue";
const qs = require('qs');

class T_cmd {
    private self: any;

    // 主组件created里初始化
    // public init(self: any) {
    //     this.self = self;
    // }

    private async webPost<Tres, Treq>(
        url: string,
        p: Treq,
        options: any = null
    ): Promise<[Tres | undefined, string | undefined]> {
        const resp = await this.httpPost(url, p, options);
        if (!resp) {
            return [undefined, `-507,FE,50,Invalid parameter![webPost]`];
        }
        const d = resp?.data?.data;
        if (d && typeof d === 'string' && Tsrv.utils.isJSONString(d)) {
            resp.data.data = JSON.parse(d);
        }
        return [resp?.data as Tres, undefined];
    }

    private async httpPost(url: string, d: any, options: any = {}): Promise<any> {
        // const _p: Treq = this.XEUtils.clone(p, true);
        // _p['cancel'] && delete _p['cancel'];
        // _p['url'] && delete _p['url'];
        const params = qs.stringify(d);
        if (Tsrv.noValue(options)) {
            // options = { headers: { Authorization: `Bearer ${lexicon.userInfo.token}` } };
            options = { headers: { Authorization: `Basic ${lexicon.userInfo.key}` } };
        }
        return await axios.post(url, params, options);
    }

    /**
     * @description 触发事件
     * @param d : 事件传参
     */
    public async doEvent(d: any): Promise<[boolean, any, any, string]> {
        const p = {
            dbName: lexicon.sysInfo.dbName,
            params: JSON.stringify(d)
        };
        const [_res, _err] = await this.webPost<any, any>(lexicon.url.event, p);
        if (_err) {
            return [false, _res, _res?.data, _res?.msg ? _res.msg : _err];
        }
        if (_res.success) {
            return [true, _res, _res?.data, _res?.msg];
        }
        return [false, _res, _res?.data, _res?.msg];
    }

    /**
     * @description 获取数据
     * @param {string} apiName 事件名称：cmd
     * @param {string} projectID 项目编号 ：例如  'xf636'
     * @param {any} params 参数 : 可自定义传参
     */
    public async getHomeData(apiName: string, projectID: string, params: any): Promise<[boolean, any, any, string]> {
        const p = {
            apiName,
            dbName: lexicon.sysInfo.dbName,
            projectID,
            params
        }
        const [_res, _err] = await this.webPost<any, any>(lexicon.url.scadaQueryData, p);
        if (_err) {
            return [false, _res, _res?.data, _res?.msg ? _res.msg : _err];
        }
        if (_res.success) {
            return [true, _res, _res?.data, _res?.msg]
        }
        return [false, _res, _res?.data, _res?.msg];
    }

    /**
     * @description 获取导航数据 （八大模块）
     */
    public async getNavData(): Promise<[boolean, any, any, string]> {
        const p = {
            dbName: lexicon.sysInfo.dbName,
            params: JSON.stringify({})
        }
        const [_res, _err] = await this.webPost<any, any>(lexicon.url.getSysInfo, p);
        if (_err) {
            return [false, _res, _res?.data, _res?.msg ? _res.msg : _err];
        }
        if (_res.success) {
            return [true, _res, _res?.data, _res?.msg]
        }
        return [false, _res, _res?.data, _res?.msg];
    }

    /**
     * @description 获取表格数据
     */
    public async getData(url: string, p: any): Promise<[boolean, any, any, string]> {
        const [resp, err] = await this.webPost<any, any>(
            url,
            p
          );
          if (err) {
            return [false, resp, resp?.data.rows, resp?.msg ? resp?.msg : err];
          }
          if (resp.success) {
            return [true, resp, resp?.data.rows, resp?.msg];
          }
          return [false, resp, resp?.data?.rows, resp?.msg];
    }


    public getTableData(funName: string, filter: any): Promise<any> {
        const pageSize = 99999;
        const pageNo = 1;
        const gpd = new ts.T_getPartDataArg();
        gpd
        .setFunName(funName)
        .setLang('zh-CN');
        gpd.setPage(pageNo, pageSize).setDbType(lexicon.sysInfo.dbType);
        gpd.setGlobalSearch('');
        gpd.setFilter(filter);
        // this.sortArr.length > 0 && gpd.setSort(this.sortArr as ifs.I_sort[]);
        gpd.setSort([]);
        // resolve(true);
        const p = XEUtils.clone(gpd, true);
        p['url'] && delete p['url'];
        const d = {
            dbName: lexicon.sysInfo.dbName,
            params: JSON.stringify(p)
        }
        return this.getData(lexicon.url.getPartData, d);
    }
}

export interface I_cmd extends T_cmd {}
export const Tcmd = new T_cmd();